package com.example.sangong.service.impl;

import cn.hutool.core.util.IdUtil;
import com.example.sangong.dao.PersonDao;
import com.example.sangong.entity.Person;
import com.example.sangong.myenum.DeleteFlagEnum;
import com.example.sangong.service.TranctionTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import javax.annotation.Resource;
import java.time.LocalDate;

@Service
@Slf4j
public class TranctionTestServiceImpl implements TranctionTestService {
    @Resource
    private PersonDao personDao;

    @Autowired
    private TranctionTestService tranctionTestService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addCaseOne() {
        tranctionTestService.addWithRequiredPropagationOne();

        try {
            tranctionTestService.addWithRequiredPropagationTwo();
        } catch (Exception e) {
            log.error("插入数据异常了", e);
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addCaseTwo() {
        tranctionTestService.addWithRequiredPropagationOne();

        try {
            tranctionTestService.addWithNestedPropagationThree();
        } catch (Exception e) {
            log.error("插入数据异常了", e);
        }
    }


    /**
     * 入口方法没有事务，则两个方法回分别开启自己的事务，因为都是
     */
    @Override
    public void addCaseThree() {
        tranctionTestService.addWithRequiredPropagationOne();

        try {
            tranctionTestService.addWithRequiredPropagationTwo();
        } catch (Exception e) {
            log.error("插入数据异常了", e);
        }
    }


    /**
     * 整体不成功，nested也不成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addCaseFour() {
        tranctionTestService.addWithRequiredPropagationOne();

        try {
            tranctionTestService.addWithNestedPropagationFour();
        } catch (Exception e) {
            log.error("插入数据异常了", e);
        }
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void testRequireNew() {
        String name = "事务3323";
        addPerson(name);
        tranctionTestService.addWithRequiredNewPropagationOne();
        int i = 1 / 0;

    }

    @Override
    public void testSupports() {
        tranctionTestService.addWithSupportsPropagationOne();
    }

    @Override
    @Transactional
    public void testSupports2() {
        tranctionTestService.addWithSupportsPropagationOne();
    }

    @Override
    public void testMandatory() {
        tranctionTestService.addWithMandatoryPropagationOne();
    }

    @Override
    @Transactional
    public void testNever() {
        tranctionTestService.addWithNeverPropagationOne();

    }

    @Override
    @Transactional

    public void testNotSupports() {
        tranctionTestService.addWithNotSupportedPropagationOne();
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public void addWithSupportsPropagationOne() {
        String name = "事务3323";
        addPerson(name);
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.MANDATORY)
    public void addWithMandatoryPropagationOne() {
        String name = "事务3323";
        addPerson(name);
    }

    @Override
    @Transactional(propagation = Propagation.NEVER)
    public void addWithNeverPropagationOne() {
        String name = "事务3323";
        addPerson(name);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void addWithNotSupportedPropagationOne() {
        String name = "事务3323";
        addPerson(name);
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addWithRequiredNewPropagationOne() {
        String name = "事务1";
        addPerson(name);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addWithRequiredPropagationOne() {

        String name = "事务1";
        addPerson(name);

    }

    private void addPerson(String name) {
        System.err.println("当前事务id为：" + TransactionSynchronizationManager.getCurrentTransactionName());

        Person person = new Person();
        person.setId(IdUtil.getSnowflake().nextId());
        person.setName(name);
        person.setAge(33);
        person.setGender(1);
        person.setBirthDay(LocalDate.now());
        person.setDeleteFlag(DeleteFlagEnum.EFFECTIVE.getCode());
        personDao.insert(person);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addWithRequiredPropagationTwo() {
        String name = "事务2";
        addPerson(name);
        int i = 1 / 0;

    }

    @Transactional(propagation = Propagation.NESTED)
    public void addWithNestedPropagationThree() {
        String name = "事务3";
        addPerson(name);
        int i = 1 / 0;

    }

    @Transactional(propagation = Propagation.NESTED)
    public void addWithNestedPropagationFour() {
        String name = "事务3";
        addPerson(name);


    }
}
